package com.doubleview.demo

/**
 * @author huchengchao <huchengchao@kuaishou.com>
 * Created on 2021-11-20
 */

//==================GPath支持=======================
def listOfMaps = [['a': 11, 'b': 12], ['a': 21, 'b': 22]]
assert listOfMaps.a == [11, 21] //GPath 标记
assert listOfMaps*.a == [11, 21] //扩展点符号

listOfMaps = [['a': 11, 'b': 12], ['a': 21, 'b': 22], null]
assert listOfMaps*.a == [11, 21, null] // 适用于空值
assert listOfMaps*.a == listOfMaps.collect { it?.a } //等价符号
// 但这只会收集非空值
assert listOfMaps.a == [11, 21]


//=====================扩展操作符=======================
assert ['z': 900,
        *  : ['a': 100, 'b': 200], 'a': 300] == ['a': 300, 'b': 200, 'z': 900]
//在map定义中的扩展map符号
assert [*: [3: 3, *: [5: 5]], 7: 7] == [3: 3, 5: 5, 7: 7]

def f = { [1: 'u', 2: 'v', 3: 'w'] }
assert [*: f(), 10: 'zz'] == [1: 'u', 10: 'zz', 2: 'v', 3: 'w']
//函数参数中的扩展map符号
f = { map -> map.c }
assert f(*: ['a': 10, 'b': 20, 'c': 30], 'e': 50) == 30

f = { m, i, j, k -> [m, i, j, k] }
//使用具有混合未命名和命名参数的展开map符号
assert f('e': 100, *[4, 5], *: ['a': 10, 'b': 20, 'c': 30], 6) ==
        [["e": 100, "b": 20, "c": 30, "a": 10], 4, 5, 6]


//====================== *运算符 =======================
assert [1, 3, 5] == ['a', 'few', 'words']*.size()

class Person {
    String name
    int age
}

def persons = [new Person(name: 'Hugo', age: 17), new Person(name: 'Sandra', age: 19)]
assert [17, 19] == persons*.age

//=================== 下标运算符切片 =======================
def text = 'nice cheese gromit!'
def x = text[2]

assert x == 'c'
assert x.class == String

def sub = text[5..10]
assert sub == 'cheese'

def list = [10, 11, 12, 13]
def answer = list[2, 3]
assert answer == [12, 13]
//您可以使用Range提取集合的一部分：
list = 100..200
sub = list[1, 3, 20..25, 33]
assert sub == [101, 103, 120, 121, 122, 123, 124, 125, 133]

//可以使用负指数从List，array，String等结尾计数
text = "nice cheese gromit!"
x = text[-1]
assert x == "!"

def name = text[-7..-2]
assert name == "gromit"

//如果使用向后Range（开始索引大于结束索引），则答案将反过来
text = "nice cheese gromit!"
name = text[3..1]
assert name == "eci"